<?php

//ToDo +Errors classes

/**
 * @link http://code.google.com/apis/accounts/docs/AuthSub.html
 *
 * @author Druid
 */
class SA_Google_Autentification {

    /**
     *
     * @var type string
     */
    protected $_token = NULL;
    protected $_userName;
    protected $_userPassword;
    protected $_serviceName;
    protected $_accountType = 'HOSTED_OR_GOOGLE';
    protected $_source = SA_Version;
    protected $_url = 'https://www.google.com/accounts/ClientLogin';
    protected $_error = '';
    protected $_isLoged = FALSE;
    const SA_Google_Analytics_Data_API = 'analytics';
    const SA_Google_Apps_API = 'app';
    const SA_Google_Base_Data_API = 'gbase';
    const SA_Google_Sites_Data_API = 'jotspot';
    const SA_Blogger_Data_API = 'blogger';
    const SA_Book_Search_Data_API = 'print';
    const SA_Calendar_Data_API = 'cl';
    const SA_Google_Code_Search_Data_API = 'codesearch';
    const SA_Contacts_Data_API = 'cp';
    const SA_Documents_List_Data_API = 'writely';
    const SA_Finance_Data_API = 'finance';
    const SA_Gmail_Atom_feed = 'mail';
    const SA_Maps_Data_APIs = 'local';
    const SA_Picasa_Web_Albums_Data_API = 'lh2';
    const SA_Sidewiki_Data_API = 'annotateweb';
    const SA_Spreadsheets_Data_API = 'wise';
    const SA_Webmaster_Tools_API = 'sitemaps';
    const SA_YouTube_Data_API = 'youtube';

    /**
     *
     * @param type $userName string
     * @param type $userPassword string
     * @param type $serviceName  string
     */
    public function SA_Google_Autentification($userName, $userPassword,
                                              $serviceName) {
        $this->_userName = $userName;
        $this->_userPassword = $userPassword;
        $this->_serviceName = $serviceName;
    }

    /**
     *
     * @param type $userName string
     * @param type $userPassword string
     * @param type $serviceName string
     */
    public function setUserData($userName, $userPassword, $serviceName) {
        $this->_userName = $userName;
        $this->_userPassword = $userPassword;
        $this->_serviceName = $serviceName;
        $this->_token = NULL;
    }

    /**
     *
     * @return type string
     */
    public function getError() {
        return $this->_error;
    }

    public function login() {
        try {
            $this->_token = '';
            $this->_error = '';
            $this->_isLoged = FALSE;
            $postParam = array('accountType' => $this->_accountType,
                'Email' => $this->_userName,
                'Passwd' => $this->_userPassword,
                'service' => $this->_serviceName,
                'source' => $this->_source);
            $request = curl_init($this->_url);
            curl_setopt($request, CURLOPT_POST, true);
            curl_setopt($request, CURLOPT_POSTFIELDS, $postParam);
            curl_setopt($request, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
            $responce = curl_exec($request);
            curl_close($request);
            if(!$responce) {
                return FALSE;
            }
            if(strpos($responce, 'Error=') !== FALSE) {
                preg_match('/Error=([A-za-z0-9_\-]+)/', $responce, $matches);
                $this->_error = $matches[1];
                return FALSE;
            }
            preg_match('/Auth=([A-za-z0-9_\-]+)/', $responce, $matches);
            $this->_token = $matches[1];
            $this->_isLoged = TRUE;
            return TRUE;
        }
        catch(Exception $e) {

            $this->_error = $e->getMessage();
            return FALSE;
        }
    }

    public function getToken() {
        return $this->_token;
    }

    public function isLoged() {
        return $this->_isLoged;
    }

    public function getServiceName() {
        return $this->_serviceName;
    }

}